﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Twinkle.Dialogs
{
    public partial class ApiViewer : Form
    {
        Timer t = null;
        public ApiViewer()
        {
            InitializeComponent();
        }

        private void ApiViewer_Load(object sender, EventArgs e)
        {
            t = new Timer();
            t.Interval = 2000;
            t.Tick += new EventHandler(t_Tick);
            t.Start();
            t_Tick(sender, e);
        }

        void t_Tick(object sender, EventArgs e)
        {
            if (!this.IsHandleCreated) return;
            if (Kernel.Config.Accounts == null) return;
            List<ListViewItem> items = new List<ListViewItem>();
            foreach (var a in Kernel.Config.Accounts)
            {
                if (a.UpdatesDinamically)
                {
                    int HTLParam = a.HomeTimelineReceivePercent;
                    if (Kernel.Plugin.TOperation.Status.RecvGlobalTimeline)
                        HTLParam = a.HomeTimelineReceivePercent * 2;
                    int DMParam = a.DMReceivePercent;
                    if (Kernel.Config.RecvFavTimeline)
                        DMParam = a.DMReceivePercent * 2;
                    int ListParam = a.ListReceivePercent;
                    if (a.ReceiveReservedLists != null)
                        ListParam *= a.ReceiveReservedLists.Length;
                    else
                        ListParam = 0;

                    double UseAPICountPerRecv = (HTLParam + a.ReplyFeedReceivePercent + DMParam + ListParam) / 100.0;
                    double ch = a.RateLimitRemain * a.ApiUseLimit / 100.0;
                    if (a.ApiRealConsumption > UseAPICountPerRecv)
                        ch /= a.ApiRealConsumption;
                    else
                        ch /= UseAPICountPerRecv;
                    double remmin = (a.RateLimitReset - DateTime.Now).TotalMinutes;
                    double minperrecv = 0;
                    if (remmin < 0 || a.RateLimitRemain == 0)
                    {
                        minperrecv = 1;
                    }
                    else
                    {
                        //残り時間 / API残り
                        minperrecv = remmin / ch;
                        if (minperrecv < 0.3)
                            minperrecv = 0.3;
                        if (minperrecv > 10)
                            minperrecv = 10;
                    }
                    items.Add(new ListViewItem(new string[]{
                    a.UserId,
                    "dynamic",
                    a.RateLimitRemain.ToString() + "/" + a.RateLimitMax.ToString(),
                    a.RateLimitReset.ToString(),
                    //API利用間隔
                    minperrecv.ToString("0.00") + (minperrecv < 1 ? (minperrecv * 60).ToString(" (0.0 sec)") : ""),
                    //1クールあたりのAPI消費数
                    UseAPICountPerRecv.ToString("0.00"),
                    //実際の消費数
                    a.ApiRealConsumption.ToString()}));
                }
                else
                {
                    items.Add(new ListViewItem(new[]{
                    a.UserId,
                    "static",
                    a.RateLimitRemain.ToString() + "/" + a.RateLimitMax.ToString(),
                    a.RateLimitReset.ToString(), "-", "-",
                    //実際の消費数
                    a.ApiRealConsumption.ToString() + "/min"}));
                }
            }
            mainView.SuspendLayout();
            int selI = -1;
            if (mainView.SelectedIndices.Count > 0)
                selI = mainView.SelectedIndices[0];
            mainView.Items.Clear();
            mainView.Items.AddRange(items.ToArray());
            if (selI > 0 && selI < mainView.Items.Count)
                mainView.SelectedIndices.Add(selI);
            mainView.ResumeLayout();
        }

        private void ApiViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (t != null)
                t.Dispose();
        }


    }
}
